package cn.wit.luohui.day13;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
	public static void main(String[] args) {
		//host:主机  port:端口
		
		try (
			Socket socket=new Socket("127.0.0.1",6666);//创建客户端socket,因为是本地,ip地址就是127.0.0.1
		//os:字符流  DataOutputStream：数据输出流，添加缓冲
			DataOutputStream os=new DataOutputStream(
				new BufferedOutputStream(socket.getOutputStream()));
			//System.in:控制台 添加缓冲
		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in,"utf-8"));
		){
			
			System.out.println("和服务器已经连上了。。。");
		while(true) {
			//从控制台读取一行文字
			String message=reader.readLine();
			os.writeUTF(message);//写入一个
			os.flush();//刷新
			
			if("88".equals(message)) {//如果写入88，则关闭Socket(即客户端关闭)
				break;
			}
		}
		System.out.println("客户端结束。。。。");
		
       } catch (UnknownHostException e) {//主机异常
			
			e.printStackTrace();

		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

}
